package com.video.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.video.entity.*;
import com.video.service.CourseService;
import com.video.service.SpeakerService;
import com.video.service.SubjectService;
import com.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private SpeakerService speakerService;

    @RequestMapping("showVideo")
    public String showVideo(Integer videoId, String subjectName, Model model) {
        System.out.println(videoId + subjectName);

        List<Subject> subjectList = subjectService.findAll();
        model.addAttribute("subjectList",subjectList);

        Video videos = videoService.findVideoById(videoId);
        model.addAttribute("video",videos);
        System.out.println(videos);
        model.addAttribute("subjectName",subjectName);
//        model.addAttribute("course",course);
        Integer courseId = videos.getCourseId();
        Course course = courseService.findCourseById(courseId);
        model.addAttribute("course",course);
        return "/before/section.jsp";
    }


    @RequestMapping("list")
    public String videoList(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize,
                            QueryVo queryVo, Model model) {

        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);

        model.addAttribute("queryVo", queryVo);

        if(pageNum<1){
            pageNum = 1;
        }else if(pageNum>getTotalPage()){
            pageNum = getTotalPage();
        }

        PageHelper.startPage(pageNum, pageSize);

        List<Video> videos = videoService.findByQueryVo(queryVo);
        PageInfo<Video> videoPageInfo = new PageInfo<>(videos);

        model.addAttribute("videoPageInfo", videoPageInfo);

        return "/behind/videoList.jsp";

    }

    public Integer getTotalPage(){
        Integer pageSize = 2;
        Integer totalCount = videoService.getTotalCount();

        if(totalCount%pageSize == 0){
            return totalCount/pageSize;
        }else {
            return totalCount/pageSize+1;
        }
    }
//------------------------------------------------------------------------
    @RequestMapping("addVideo")
    public String addVideo(Video video, Model model,Course course) {

        List<Course> courseList1 = courseService.findAll();
        model.addAttribute("courseList",courseList1);

        List<Speaker> speakerList1 = speakerService.findAll();
        model.addAttribute("speakerList",speakerList1);

        List<Video> videoList = videoService.findAll();
        model.addAttribute("videoList",videoList);
        return "/behind/addVideo.jsp";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Integer id, Video video) {

        if (id == null) {

            videoService.addVideo(video);
        } else {

            video.setId(id);
            videoService.updateVideo(video);
        }

        return "redirect:/video/list";
    }

    @RequestMapping("videoDel")
    @ResponseBody
    public String videoDel(Integer id) {
        int i = videoService.deleteByPrimaryKey(id);
        if (i==0) {
            return "default";
        } else {
            return "success";
        }

    }

    @RequestMapping("edit")
    public String edit(Integer id, Model model) {

        Video video = videoService.findById(id);
        model.addAttribute("video", video);

        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);

        return "/behind/addVideo.jsp";
    }

    @RequestMapping("delBatchVideos")
    public String delBatchVideos(Integer[] ids) {
        for(Integer id:ids) {
            videoService.deleteByPrimaryKey(id);
        }
        return "redirect:/video/list";
    }

}

